package basics.arrays;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

/**
 * Java 数组常见操作示例
 * 
 * 本类提供了各种数组操作的实际示例，可以运行查看结果
 */
public class ArrayOperationsDemo {
    
    public static void main(String[] args) {
        System.out.println("===== Java 数组操作示例 =====");
        
        // 基本操作示例
        basicOperations();
        
        // 数组工具类示例
        arraysUtilDemo();
        
        // 常见算法示例
//        commonAlgorithms();
        
        // 高级特性示例
        advancedFeatures();
    }
    
    /**
     * 数组基本操作示例
     */
    private static void basicOperations() {
        System.out.println("\n----- 基本操作示例 -----");
        
        // 创建数组的不同方式
        int[] array1 = new int[5];
        int[] array2 = {10, 20, 30, 40, 50};
        int[] array3 = new int[]{5, 15, 25, 35, 45};
        
        // 数组赋值
        for (int i = 0; i < array1.length; i++) {
            array1[i] = i * 10;
        }
        
        // 打印数组
        System.out.println("array1: " + Arrays.toString(array1));
        System.out.println("array2: " + Arrays.toString(array2));
        System.out.println("array3: " + Arrays.toString(array3));
        
        // 数组长度
        System.out.println("array2的长度: " + array2.length);
        
        // 访问数组元素
        System.out.println("array2的第一个元素: " + array2[0]);
        System.out.println("array2的最后一个元素: " + array2[array2.length - 1]);
        
        // 二维数组示例
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        System.out.println("二维数组遍历:");
        for (int[] row : matrix) {
            for (int value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
    
    /**
     * Arrays工具类示例
     */
    private static void arraysUtilDemo() {
        System.out.println("\n----- Arrays工具类示例 -----");
        
        // 排序
        int[] numbers = {5, 2, 9, 1, 7};
        System.out.println("排序前: " + Arrays.toString(numbers));
        Arrays.sort(numbers);
        System.out.println("排序后: " + Arrays.toString(numbers));
        
        // 自定义排序（降序）
        Integer[] nums = {5, 2, 9, 1, 7};
        Arrays.sort(nums, Collections.reverseOrder());
        System.out.println("降序排序: " + Arrays.toString(nums));
        
        // 二分查找
        int[] sortedArray = {1, 2, 5, 7, 9};
        int index = Arrays.binarySearch(sortedArray, 5);
        System.out.println("5在数组中的索引: " + index);
        
        // 填充
        int[] filledArray = new int[5];
        Arrays.fill(filledArray, 10);
        System.out.println("填充后的数组: " + Arrays.toString(filledArray));
        
        // 部分填充
        int[] partialFill = {1, 2, 3, 4, 5};
        Arrays.fill(partialFill, 1, 4, 8);
        System.out.println("部分填充后的数组: " + Arrays.toString(partialFill));
        
        // 复制
        int[] original = {1, 2, 3, 4, 5};
        int[] copy = Arrays.copyOf(original, 5);
        int[] extended = Arrays.copyOf(original, 8);
        int[] partial = Arrays.copyOfRange(original, 1, 4);
        
        System.out.println("原始数组: " + Arrays.toString(original));
        System.out.println("复制数组: " + Arrays.toString(copy));
        System.out.println("扩展数组: " + Arrays.toString(extended));
        System.out.println("部分复制: " + Arrays.toString(partial));
        
        // 比较
        int[] array1 = {1, 2, 3};
        int[] array2 = {1, 2, 3};
        int[] array3 = {1, 2, 4};
        
        System.out.println("array1与array2相等? " + Arrays.equals(array1, array2));
        System.out.println("array1与array3相等? " + Arrays.equals(array1, array3));
        
        // 多维数组比较
        int[][] matrix1 = {{1, 2}, {3, 4}};
        int[][] matrix2 = {{1, 2}, {3, 4}};
        System.out.println("matrix1与matrix2相等? " + Arrays.deepEquals(matrix1, matrix2));
    }
    
    /**
     * 常见数组算法示例
     */
    private static void commonAlgorithms() {
        System.out.println("\n----- 常见算法示例 -----");
        
        // 数组求和
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        System.out.println("数组元素和: " + sum);
        
        // 使用Stream API求和
        int streamSum = Arrays.stream(numbers).sum();
        System.out.println("使用Stream API求和: " + streamSum);
        
        // 查找最大值和最小值
        int[] values = {5, 2, 9, 1, 7};
        int max = values[0];
        int min = values[0];
        
        for (int i = 1; i < values.length; i++) {
            if (values[i] > max) {
                max = values[i];
            }
            if (values[i] < min) {
                min = values[i];
            }
        }
        
        System.out.println("最大值: " + max);
        System.out.println("最小值: " + min);
        
        // 使用Stream API查找最大值和最小值
        int streamMax = Arrays.stream(values).max().getAsInt();
        int streamMin = Arrays.stream(values).min().getAsInt();
        
        System.out.println("使用Stream API查找最大值: " + streamMax);
        System.out.println("使用Stream API查找最小值: " + streamMin);
        
        // 数组反转
        int[] original = {1, 2, 3, 4, 5};
        int[] reversed = new int[original.length];
        
        for (int i = 0; i < original.length; i++) {
            reversed[i] = original[original.length - 1 - i];
        }
        
        System.out.println("原始数组: " + Arrays.toString(original));
        System.out.println("反转后的数组: " + Arrays.toString(reversed));
        
        // 数组合并
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        int[] merged = new int[array1.length + array2.length];
        
//        System.arraycopy(array1, 0, mered, 0, array1.length);
//        System.arraycopy(array2, 0, mergged, array1.length, array2.length);
//
//        System.out.println("合并后的数组: " + Arrays.toString(merged));
    }
    
    /**
     * 数组高级特性示例
     */
    private static void advancedFeatures() {
        System.out.println("\n----- 高级特性示例 -----");
        
        // 可变参数
        System.out.println("可变参数求和: " + sum(1, 2, 3, 4, 5));
        System.out.println("可变参数求和: " + sum(10, 20, 30));
        System.out.println("可变参数求和: " + sum(new int[]{1, 2, 3, 4, 5, 6}));
        
        // 数组与集合的转换
        String[] array = {"Java", "Python", "C++"};
        
        // 数组转List
        List<String> list = Arrays.asList(array);
        System.out.println("数组转List: " + list);
        
        // 创建可变List
        List<String> mutableList = new ArrayList<>(Arrays.asList(array));
        mutableList.add("JavaScript");
        System.out.println("可变List: " + mutableList);
        
        // List转数组
        List<String> languages = new ArrayList<>();
        languages.add("Java");
        languages.add("Python");
        
        String[] langArray = languages.toArray(new String[0]);
        System.out.println("List转数组: " + Arrays.toString(langArray));
        
        // 数组克隆
        int[] original = {1, 2, 3, 4, 5};
        int[] clone = original.clone();
        
        System.out.println("原始数组: " + Arrays.toString(original));
        System.out.println("克隆数组: " + Arrays.toString(clone));
        
        // 修改克隆数组不影响原始数组
        clone[0] = 100;
        System.out.println("修改后的克隆数组: " + Arrays.toString(clone));
        System.out.println("原始数组保持不变: " + Arrays.toString(original));
    }
    
    /**
     * 可变参数示例方法
     */
    private static int sum(int... numbers) {
        int total = 0;
        for (int num : numbers) {
            total += num;
        }
        return total;
    }
} 